package desi.bakar.coreservice;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class ValueObjectMapper {

    private ObjectMapper objectMapper;


    public ValueObjectMapper() {
        objectMapper = configureMapper();

    }

    private ObjectMapper configureMapper() {
        ObjectMapper objectMapper = new ObjectMapper()
                .addHandler(new ValueObjectRawDataPopulater());

        return objectMapper;

    }

    public void writeObject(Writer writer, Object o) {
        try {
            objectMapper.writeValue(writer, o);
        } catch (IOException e) {
            throw new RuntimeException(e);

        }
    }

    public <T> T readObject(Reader reader, Class<T> type) {
        try {
            return objectMapper.readValue(reader, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
